package com.demo.modalframe4;

import com.demo.modalframe4.listener.CloseWindowListener;
import com.demo.modalframe4.utils.GraphicsUtil;
import com.demo.modalframe4.utils.UIUtil;
import com.esotericsoftware.tablelayout.swing.Table;

import javax.swing.*;
import java.awt.*;

public class Frame2 extends JFrame {
    public Frame2() {
        initFrame();

        this.setTitle("Frame 2(parent frame)");
        this.setMinimumSize(new Dimension(GraphicsUtil.Frame_Width, GraphicsUtil.Frame_Height));
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.addWindowListener(new CloseWindowListener());
    }

    private void initFrame() {
        JButton button1 = new JButton("Modalless Dialog");
        button1.addActionListener(e -> {
            showModallessDialog();
        });

        JButton button2 = new JButton("Document Dialog");
        button2.addActionListener(e -> {
            showDocumentModalDialog();
        });

        JButton button3 = new JButton("Application Dialog");
        button3.addActionListener(e -> {
            showApplicationModalDialog();
        });

        Table contentTable = UIUtil.defaultTable();
        contentTable.addCell("打开一个非模态对话框:").uniform();
        contentTable.addCell(button1).row();

        contentTable.addCell("打开一个Document模态对话框:").uniform();
        contentTable.addCell(button2).row();

        contentTable.addCell("打开一个Application模态对话框:").uniform();
        contentTable.addCell(button3).row();

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(contentTable, BorderLayout.CENTER);
    }

    private void showModallessDialog() {
        JDialog dialog = new JDialog(this, "Modalless Dialog",
                Dialog.ModalityType.MODELESS);
        dialog.getContentPane().add(new JTextArea("这是一个非模态对话框"));
        dialog.setMinimumSize(new Dimension(200, 100));
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    private void showDocumentModalDialog() {
        JDialog dialog = new JDialog(this, "Document Modal Dialog",
                Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.getContentPane().add(new JTextArea("这是一个Document模态对话框"));
        dialog.setMinimumSize(new Dimension(200, 100));
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    private void showApplicationModalDialog() {
        JDialog dialog = new JDialog(this, "Application Modal Dialog",
                Dialog.ModalityType.APPLICATION_MODAL);
        dialog.getContentPane().add(new JTextArea("这是一个Application模态对话框"));
        dialog.setMinimumSize(new Dimension(200, 100));
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
}
